/*
 * PluginResult.cpp
 *
 *  Created on: 2011. 3. 15.
 *      Author: JeeHoon
 */
#include "api/PluginResult.h"
#include <stdlib.h>

using namespace Osp::Base;

const char * PluginResult::PluginResultText[] = {
	"No result",
	"OK",
	"Class not found",
	"Illegal access",
	"Instantiation error",
	"Malformed url",
	"IO error",
	"Invalid action",
	"JSON error",
	"Error",
};

PluginResult::PluginResult()
{
	__status = NO_RESULT;
	__keepCallback = false;
}

PluginResult::~PluginResult()
{
}

result
PluginResult::inti(Status status)
{
	__status = status;
	__message = L"'" + getStateText() + L"'";
	return E_SUCCESS;
}

result
PluginResult::inti(Status status,const mchar * message)
{
	return inti(status, String(message));
}

result
PluginResult::inti(Status status, Osp::Base::String message)
{
	result r = E_SUCCESS;

	char * msg = NULL;
	cJSON * jsonMsg = NULL;
	char * out = NULL;

	if(E_SUCCESS == r)
	{
		msg = StringToChar(message);
		if(NULL == msg)
		{
			r = E_OUT_OF_MEMORY;
		}
	}

	if(E_SUCCESS == r)
	{
		jsonMsg = cJSON_CreateString(msg);
		if(NULL == jsonMsg)
		{
			r = E_FAILURE;
		}
		else
		{
			free(msg);
			msg = NULL;
		}
	}

	if(E_SUCCESS == r)
	{
		out = cJSON_Print(jsonMsg);
		if(NULL == out)
		{
			r = E_FAILURE;
		}
		else
		{
			cJSON_Delete(jsonMsg);
			jsonMsg = NULL;
		}
	}

	if(E_SUCCESS == r)
	{
		__status = status;
		__message = String(out);
		free(out);
		out = NULL;
	}

	if(NULL != out)
		free(out);

	if(NULL != msg)
		free(msg);

	if(NULL != jsonMsg)
		cJSON_Delete(jsonMsg);

	return r;
}

result
PluginResult::inti(Status status, cJSON * jsonMessage, Osp::Base::String cast = L"")
{
	result r = E_SUCCESS;
	char * out = NULL;

	__status = status;
	__cast = cast;

	if(E_SUCCESS == r)
	{
		out = cJSON_Print( jsonMessage );
		if(NULL == out)
		{
			r = E_FAILURE;
		}
	}

	if(E_SUCCESS == r)
	{
		__message = String(out);
		free(out);
		out = NULL;
	}

	if(NULL != out)
		free(out);

	return r;
}

result
PluginResult::inti(Status status, int i)
{
	__status = status;
	__message = Integer::ToString(i);
	return E_SUCCESS;
}

result
PluginResult::inti(Status status, float f)
{
	__status = status;
	__message = Float::ToString(f);
	return E_SUCCESS;
}

result
PluginResult::inti(Status status, bool b)
{
	__status = status;
	__message = ( b ? L"true" : L"false" );
	return E_SUCCESS;
}

void
PluginResult::setStatus(Status status)
{
	__status = status;
}

void
PluginResult::setMessage(Osp::Base::String message)
{
	__message = message;
}

void
PluginResult::setKeepCallback(bool keepCallback)
{
	__keepCallback = keepCallback;
}

Status
PluginResult::getStatus()
{
	return __status;
}

String
PluginResult::getMessage()
{
	return __message;
}

bool
PluginResult::getKeepCallback()
{
	return __keepCallback;
}

String
PluginResult::getJSONString()
{
	String stateNumber;
	stateNumber.Format(10,L"%d",__status);

	return L"{status:" + stateNumber + L",message:" + __message  + L",keepCallback:" + (__keepCallback ? L"true": L"false") + L"}";
}

String
PluginResult::toSuccessCallbackString(String callbackId)
{
	String output;

	if ( __cast != L"" )
	{
		output = L"var temp = " + __cast + L"("+ getJSONString() + L");\n";
		output += L"PhoneGap.callbackSuccess('" + callbackId  + L"',temp);";
	}
	else
	{
		output = L"PhoneGap.callbackSuccess('" + callbackId + L"'," + getJSONString() + L");";
	}
	return output;
}

String
PluginResult::toErrorCallbackString(String callbackId)
{
	return L"PhoneGap.callbackError('" + callbackId + L"', " + getJSONString() + L");";
}

String
PluginResult::getStateText()
{
	return String(PluginResultText[__status]);
}
